package com.zlsy.thread.lock.lock;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhouliang
 * @date 2020/7/27 21:07
 */
public class TryLockDeadLock implements Runnable {
    int flag = 1;
    private ReentrantLock lock1 = new ReentrantLock();
    private ReentrantLock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        TryLockDeadLock r1 = new TryLockDeadLock();
        r1.flag = 1;
        TryLockDeadLock r2 = new TryLockDeadLock();
        r2.flag = 2;
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();
    }

    @Override
    public void run() {
        if (flag == 1) {
            for (int i = 0; i < 100; i++) {
                try {
                    System.err.println("线程1尝试获取锁1。");
                    if (lock1.tryLock(800, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println("线程1获取到了锁1。");
                            Thread.sleep(new Random().nextInt(1000));
                        } finally {
                            lock1.unlock();
                            System.out.println("线程1释放了锁1");
                        }

                        System.err.println("线程1尝试获取锁2。");
                        if (lock2.tryLock(3000, TimeUnit.MILLISECONDS)) {
                            try {
                                System.out.println("线程1获取到了锁2。");
                                Thread.sleep(new Random().nextInt(1000));
                                break;
                            } finally {
                                lock2.unlock();
                                System.out.println("线程1释放了锁2");
                            }
                        } else {
                            System.out.println("线程1未获取到锁2，已重试！");
                        }
                    } else {
                        System.out.println("线程1未获取到锁1，已重试！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        if (flag == 2) {
            for (int i = 0; i < 100; i++) {
                try {
                    System.err.println("线程2尝试获取锁2。");
                    if (lock2.tryLock(800, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println("线程2获取到了锁2。");
                            Thread.sleep(new Random().nextInt(1000));
                        } finally {
                            System.out.println("线程2释放了锁2");
                            lock2.unlock();
                        }
                        System.err.println("线程2尝试获取锁1。");
                        if (lock1.tryLock(800, TimeUnit.MILLISECONDS)) {
                            try {
                                System.out.println("线程2获取到了锁1。");
                                Thread.sleep(new Random().nextInt(1000));
                                break;
                            } finally {
                                lock1.unlock();
                                System.out.println("线程2释放了锁1");
                            }
                        } else {
                            System.out.println("线程2未获取到锁1，已重试！");
                        }
                    } else {
                        System.out.println("线程2未获取到锁2，已重试！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
